package com.example.searchenginebackend.controller;
import com.example.searchenginebackend.common.ErrorCode;
import com.example.searchenginebackend.common.Result;
import com.example.searchenginebackend.exception.ServiceException;
import com.example.searchenginebackend.model.DocInfo;
import com.example.searchenginebackend.service.DocService;
import com.example.searchenginebackend.utls.ResultUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin // 解决跨域问题
@RestController
@RequestMapping("/doc")
public class DocController {
    @Resource
    private DocService docService;
    @GetMapping("/search")
    public Result<List<DocInfo>> searchDocInfos(@RequestParam(required = false, defaultValue = "1") Long pageNum, @RequestParam(required = false, defaultValue = "10") Long pageSize, String query) {
        // 1. 参数校验
        if (StringUtils.isBlank(query)) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 2. 进行搜索
        List<DocInfo> docInfos = docService.search(query);
        return ResultUtils.success(docInfos);
    }
}
